#include "qipan.h"
#include <QDebug>
qipan::qipan(QWidget *parent) : QWidget(parent),
    m_hoverFlg(false),
    flag(0)
{
    /* 清除脏数据 */
    m_board = (Role **)malloc(sizeof(Role *) * m_boardSize);
    for(int idx=0;idx< m_boardSize;idx++)
    {
        m_board[idx] = (Role *)malloc(sizeof(Role)*m_boardSize);
    }

//    for(int row=0;row<m_boardSize;row++)
//    {
//        for(int col=0;col< m_boardSize; col++)
//        {
//            m_board[row][col]= EMPTY;
//        }
//    }
    initBoard();


    /* 鼠标跟随事件 */
    setMouseTracking(true);
    m_hoverCol = 0;
    m_hoverRow = 0;

}

void qipan::setBoardInfo(int row, int col, Role role)
{
    if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize))
    {
        if(m_board[row][col] == EMPTY)
        {
            m_board[row][col] = role;
            qDebug() << "role " << role << endl;
        }
    }
}

Role **qipan::getBoardInfo(int &boardSize)
{
    boardSize = m_boardSize;
    return  reinterpret_cast<Role **> (m_board);
}

void qipan::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    /* 启用抗锯齿 */
    painter.setRenderHint(QPainter::Antialiasing);  // 启用抗锯齿效果

    // 绘制棋盘背景
    // 棋盘背景颜色
    QColor backgroundColor = QColor(135, 206, 235);  // 天青色，对应RGB值大致设置（可根据实际需求微调）
    painter.fillRect(rect(), backgroundColor);
    /* 绘制棋盘的网格线 */
    for(int idx = 0;idx < m_boardSize ;idx++)
    {
        /* 绘制竖线 */
        painter.drawLine(m_magin + idx * m_cellSize,m_magin , m_magin + idx * m_cellSize,m_magin + (m_boardSize - 1) * m_cellSize);

        /* 绘制横线 */
        painter.drawLine(m_magin,m_magin + idx * m_cellSize,m_magin +( m_boardSize - 1 )* m_cellSize,m_magin + idx * m_cellSize);

    }

    /* 绘制悬停点 */
    if(m_hoverFlg && (m_hoverRow >= 0 && m_hoverRow < m_boardSize) &&
                       (m_hoverCol >= 0 && m_hoverCol < m_boardSize) &&
                        m_board[m_hoverRow][m_hoverCol] == EMPTY)
    {
        painter.setBrush(Qt::red);
        painter.drawEllipse(QPoint( m_magin + m_hoverRow * m_cellSize , m_magin + m_hoverCol * m_cellSize),m_cellSize / 5,m_cellSize / 5);

    }

    /* 绘制棋子 */
    for(int row = 0;row < m_boardSize;row++)
    {
        for(int col = 0;col < m_boardSize;col++)
        {
            if(m_board[row][col] != EMPTY)
            {
                if(m_board[row][col] == PLAYER)
                {
                    painter.setBrush(Qt::black);
                }
                else if (m_board[row][col] == COMPUTER)
                {
                    painter.setBrush(Qt::white);
                }

                painter.drawEllipse(QPoint( m_magin + row * m_cellSize , m_magin + col * m_cellSize),m_cellSize / 3,m_cellSize / 3);

            }
        }
    }



}

    /* 鼠标移动事件 */
void qipan::mouseMoveEvent(QMouseEvent *event)
{
    if(flag == 1)
    {
        int row = (event->x() - m_magin + m_cellSize / 2) / m_cellSize;
        int col = (event->y() - m_magin + m_cellSize / 2) / m_cellSize;

        /* 判断坐标是否再棋盘内部 */
        if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize))
        {
            if(m_hoverRow != row || m_hoverCol != col)
            {
                m_hoverRow = row;
                m_hoverCol = col;

                m_hoverFlg = true;

                /* 更新画面 */
                update();
            }
        }
        else
        {
            if(m_hoverFlg)
            {
                m_hoverFlg = false;

                /* 移除棋盘需要将坐标清空 */
                m_hoverRow = -1;
                m_hoverCol = -1;
                /* 更新画面 */
                update();
            }

        }
    }

}

/* 鼠标点击事件 */
void qipan::mousePressEvent(QMouseEvent *event)
{
    if(flag == 1)
    {
        int row = (event->x() - m_magin + m_cellSize / 2) / m_cellSize;
        int col = (event->y() - m_magin + m_cellSize / 2) / m_cellSize;
        if( (row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize) &&  m_board[row][col] == EMPTY)
        {
            if(m_board[row][col] == EMPTY)
            {
//                m_board[row][col] = PLAYER;

//                /* 手动更新 */
//                update();

                emit chessClick(row,col);
            }
        }
    }
}

int qipan::getBoardSize()
{
    return m_boardSize;
}

void qipan::initBoard()
{
    for(int row=0;row<m_boardSize;row++)
    {
        for(int col=0;col< m_boardSize; col++)
        {
            m_board[row][col]= EMPTY;
        }
    }
}
